home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / IPv6.php < prev    next >
PHP Script  |  2004-03-24  |  7KB  |  211 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Merz <alexander.merz@web.de>                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: IPv6.php,v 1.6 2003/01/04 11:55:47 mj Exp $
  20.  
  21. /**
  22. * Class to validate and to work with IPv6
  23. *
  24. * Todo: some optimizations for checkIPv6()
  25. *
  26. * @author  Alexander Merz <alexander.merz@t-online.de>
  27. * @package Net_IPv6
  28. * @version $Id: IPv6.php,v 1.6 2003/01/04 11:55:47 mj Exp $
  29. * @access  public
  30. */
  31. class Net_IPv6 {
  32.  
  33.     // {{{ Uncompress()
  34.         
  35.     /**
  36.      * Uncompresses an IPv6 adress
  37.      * 
  38.      * RFC 2373 allows you to compress zeros in an adress to '::'. This
  39.      * function expects an valid IPv6 adress and expands the '::' to
  40.      * the required zeros.
  41.      * 
  42.      * Example:  FF01::101    ->  FF01:0:0:0:0:0:0:101
  43.      *           ::1        ->  0:0:0:0:0:0:0:1 
  44.      *
  45.      * @access public
  46.      * @see Compress()
  47.      * @static
  48.      * @param string $ip    a valid IPv6-adress (hex format)
  49.      * @return string    the uncompressed IPv6-adress (hex format)
  50.      */
  51.     function Uncompress($ip) {
  52.         if (strstr($ip, '::') ) {
  53.             $ipComp = str_replace('::', ':', $ip);
  54.             if (':' == $ipComp{0}) {
  55.                 $ipComp = substr($ipComp, 1);
  56.             }
  57.  
  58.             $ipParts = count(explode(':', $ipComp));
  59.             if (strstr($ip, '.')) {
  60.                 $ipParts++;
  61.             }
  62.  
  63.             $ipMiss = "" ;
  64.             for ($i = 0; (8-$ipParts) > $i; $i++) {
  65.                 $ipMiss = $ipMiss.'0:';
  66.             }
  67.             if (0 != strpos($ip, '::') ) {
  68.                 $ipMiss = ':'.$ipMiss;
  69.             }
  70.  
  71.             $ip = str_replace('::', $ipMiss, $ip);
  72.         }
  73.  
  74.         return $ip;        
  75.     }
  76.     
  77.     // }}}
  78.     // {{{ Compress()
  79.  
  80.     /**
  81.      * Compresses an IPv6 adress
  82.      * 
  83.      * RFC 2373 allows you to compress zeros in an adress to '::'. This
  84.      * function expects an valid IPv6 adress and compresses successive zeros
  85.      * to '::'
  86.      * 
  87.      * Example:  FF01:0:0:0:0:0:0:101     -> FF01::101
  88.      *           0:0:0:0:0:0:0:1        -> ::1 
  89.      *
  90.      * @access public
  91.      * @see Uncompress()
  92.      * @static    
  93.      * @param string $ip    a valid IPv6-adress (hex format)
  94.      * @return string    the compressed IPv6-adress (hex format)    
  95.      */
  96.     function Compress($ip)    {
  97.  
  98.         if (!strstr($ip, "::")) {
  99.             $ipPart = explode(":", $ip);
  100.             $ipComp = "";
  101.             $flag   = true;
  102.             for ($i = 0; $i < count($ipPart); $i ++) {
  103.                 if (!$ipPart[$i] and !$ipPart[$i+1]) {
  104.                     break;
  105.                 } else {
  106.                     $ipComp = $ipComp.$ipPart[$i].":";
  107.                 }
  108.             }
  109.             $ipComp = substr($ipComp, 0, -1);            
  110.             for (; $i < count($ipPart); $i++) {
  111.                 if($flag) {
  112.                     $flag   = !$flag;
  113.                     $ipComp = $ipComp."::";
  114.                 }
  115.                 if(0 != $ipPart[$i]) {
  116.                     break;
  117.                 }
  118.             }
  119.  
  120.             for (; $i < count($ipPart); $i++) {
  121.                 $ipComp = $ipComp.$ipPart[$i].":";
  122.             }
  123.         }
  124.         if ('::' == substr($ipCom, strlen($ipcom)-2 )) {
  125.             $ip = substr($ipComp, 0, -1);
  126.         } else {
  127.             $ip = $ipComp ;
  128.         }
  129.         return $ip;
  130.  
  131.     }
  132.  
  133.     // }}}
  134.     // {{{ SplitV64()
  135.  
  136.     /**
  137.      * Splits an IPv6 adress into the IPv6 and a possible IPv4 part
  138.      *
  139.      * RFC 2373 allows you to note the last two parts of an IPv6 adress as
  140.      * an IPv4 compatible adress
  141.      *
  142.      * Example:  0:0:0:0:0:0:13.1.68.3
  143.      *           0:0:0:0:0:FFFF:129.144.52.38
  144.      *
  145.      * @access public
  146.      * @static
  147.      * @param string $ip    a valid IPv6-adress (hex format)
  148.      * @return array        [0] contains the IPv6 part, [1] the IPv4 part (hex format)
  149.      */
  150.     function SplitV64($ip) {
  151.         $ip = Net_IPv6::Uncompress($ip);
  152.         if (strstr($ip, '.')) {
  153.             $pos = strrpos($ip, ':');
  154.             $ip{$pos} = '_';
  155.             $ipPart = explode('_', $ip);
  156.             return $ipPart;
  157.         } else {
  158.             return array($ip, "");
  159.         }
  160.     }
  161.  
  162.     // }}}
  163.     // {{{ checkIPv6
  164.  
  165.     /**
  166.      * Checks an IPv6 adress
  167.      *
  168.      * Checks if the given IP is IPv6-compatible
  169.      *
  170.      * @access public
  171.      * @static
  172.      * @param string $ip    a valid IPv6-adress
  173.      * @return boolean    true if $ip is an IPv6 adress
  174.      */
  175.     function checkIPv6($ip) {
  176.  
  177.         $ipPart = Net_IPv6::SplitV64($ip);
  178.         $count = 0;
  179.         if (!empty($ipPart[0])) {
  180.             $ipv6 = explode(':', $ipPart[0]);
  181.             for ($i = 0; $i < count($ipv6); $i++) {
  182.                 $dec = hexdec($ipv6[$i]); 
  183.                 if ($ipv6[$i] >= 0 && $dec <= 65535 && $ipv6[$i] == strtoupper(dechex($dec))) {
  184.                     $count++;
  185.                 }
  186.             }
  187.             if (8 == $count) {
  188.                 return true;
  189.             } elseif (6 == $count and !empty($ipPart[1])) {
  190.                 $ipv4 = explode('.',$ipPart[1]);
  191.                 $count = 0;
  192.                 for ($i = 0; $i < count($ipv4); $i++) {
  193.                     if ($ipv4[$i] >= 0 && (integer)$ipv4[$i] <= 255 && preg_match("/^\d{1,3}$/", $ipv4[$i])) {
  194.                         $count++;
  195.                     }
  196.                 }
  197.                 if (4 == $count) {
  198.                     return true;
  199.                 }
  200.             } else {
  201.                 return false;
  202.             }
  203.  
  204.         } else {
  205.             return false;
  206.         }
  207.     }    
  208.     // }}}    
  209. }
  210. ?>
  211.